package com.gcsoftware.web.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.gcsoftware.web.service.T_COM_SYS_DICT_SERVICE;
import com.gcsoftware.web.service.T_GW_APPS_SERVICE;
import com.gcsoftware.web.beans.request.GetAppsReq;
import com.gcsoftware.web.beans.T_GW_APPS;
import com.gcsoftware.web.beans.T_GW_APP_IPS;
import com.gcsoftware.utils.log.InvokeLog;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.utils.SysConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by qs on 2018/3/20.
 */
@Controller
@RequestMapping("/apps")
public class AppController {

    @Autowired
    private T_GW_APPS_SERVICE service;

    @Autowired
    private T_COM_SYS_DICT_SERVICE dictService;

    @RequestMapping(value = "")
    public String appList(Model model) {
        model.addAttribute("appStatusList" , dictService.queryForList(SysConst.DICT_GRP_APP_STATUS));
        return "admin/app-list";
    }

    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    @ResponseBody
    public List<T_GW_APPS> queryForList(@RequestParam(value = "c_APP_NAME" , required = false) String c_APP_NAME,
                                        @RequestParam(value = "c_APP_STATUS" , required = false) String c_APP_STATUS) {
        return service.queryForList("".equals(c_APP_NAME)?null: c_APP_NAME,"".equals(c_APP_STATUS)?null: c_APP_STATUS , null);
    }

    @RequestMapping(value = "/{c_APP_ID}/ip-list" , method = RequestMethod.POST)
    @ResponseBody
    public List<T_GW_APP_IPS> queryAppIdList(@PathVariable(value = "c_APP_ID") String c_APP_ID) {
        return service.queryForIpList(c_APP_ID);
    }

    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils updateApps(@RequestBody String param){
        GetAppsReq appReq = JSONObject.parseObject(param , GetAppsReq.class); //string转json
        return service.updateApp(appReq);
    }

    @ResponseBody
    @RequestMapping(value = "/{c_APP_ID}/updatePwd" , method = RequestMethod.POST)
    @InvokeLog(name = "update pwd of app" , description = "重置应用密码" , logType = LogUtils.LOG_TYPE_OPERATE)
    public MapUtils updatePwd(@PathVariable String c_APP_ID) {
        return service.updateAppPwd(c_APP_ID);
    }

    @RequestMapping(value = "/{c_APP_ID}/offLine" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils offLine(@PathVariable String c_APP_ID) {
        return service.updateAppStatus(c_APP_ID, SysConst.DICT_APP_STATUS_OFFLINE);
    }
}
